// Misperception susceptibility
// Calculating AUC for each participant-wave based on 
// rating data and saving in a new file (AUC.dta)
// Note: also calculates "bias" but this is not the same a c in SDT

use "PanelPublic.dta", clear

// Convert to long format
// Note: id preserves the order of the original dataset, which appears to be 
// based on endtime_W1
gen id = _n
reshape long starttime_W endtime_W weight_wave_ ///
	F1e_W F2e_W F3e_W F4e_W F5e_W F6e_W F7e_W F8e_W F9e_W F10e_W ///
	T1e_W T2e_W T3e_W T4e_W T5e_W T6e_W T7e_W T8e_W T9e_W T10e_W P1e_W ///
	F1b_W F2b_W F3b_W F4b_W F5b_W F6b_W F7b_W F8b_W F9b_W F10b_W ///
	T1b_W T2b_W T3b_W T4b_W T5b_W T6b_W T7b_W T8b_W T9b_W T10b_W P1b_W ///
	F1bn_W F2bn_W F3bn_W F4bn_W F5bn_W F6bn_W F7bn_W F8bn_W F9bn_W F10bn_W ///
	T1bn_W T2bn_W T3bn_W T4bn_W T5bn_W T6bn_W T7bn_W T8bn_W T9bn_W T10bn_W P1bn_W ///
	fbuse_W rduse_W twuse_W ///
	nuse_a_W nuse_b_W nuse_c_W nuse_d_W nuse_e_W nuse_f_W, i(id) j(wave)

// Omit missing waves
drop if starttime_W == .
	
lab def w 1 "W1" 2 "W2" 3 "W3" 4 "W4" 5 "W5" 6 "W6" 7 "W7" 8 "W8" 9 "W9" ///
	10 "W10" 11 "W11" 12 "W12"
lab val wave w
xtset id wave, generic

// Just keep variables needed for for ROC
keep id wave F1b_W-P1bn_W

// Missing values
recode F1b_W-P1bn_W (8=.)

// Collapse heard and unheard into single rating for each statement
forvalues s = 1/10 {
	gen R`s'=F`s'b_W 
	replace R`s'=F`s'bn_W if R`s'==9
}
forvalues s = 1/10 {
	local s_ = `s' + 10
	gen R`s_'=T`s'b_W 
	replace R`s_'=T`s'bn_W if R`s_'==9
}

// Just keep variables needed for for ROC
keep id wave R1-R20

// Note: id2 is sequential (1-10,367) when sorted by wave (1-12) and id (1-1204)
gen id2 = _n

reshape long R, i(id2) j(state)

// Reverse so true is high
replace R=5-R
ren R Rating
gen trueVal=0
replace trueVal=1 if state>10

gen AUC=.
gen bias=.
forvalues i = 1/10367 {
	display "Next id: " `i'
	rocreg trueVal Rating if id2==`i', auc bseed(1029) breps(1000) ///
	tiecorrected nodots
	matrix AUC = e(b)
	replace AUC=AUC[1,1] if id2==`i'
	matrix bias = e(bias)
	replace bias=bias[1,1] if id2==`i'
}

// Export
duplicates drop id2, force
keep id2 wave AUC bias
save "AUC.dta", replace

